package modes

import (
	"fmt"
	"time"
)

/*
 * 描述: 题库名称介绍
 *
 *
 *****************************/

type TopicNameRe struct {
	Id     int64  `json:"n_id"`                                //ID
	SId    int64  `json:"n_sid" xorm:"sid"`                    //ID
	TypeId int    `json:"n_type" xorm:"type_id"`               //类型ID (章节练习题，历年真题)
	Name   string `json:"n_name" xorm:"name"`                  //题选项
	At     int64  `json:"n_at" xorm:"at"`                      //时间
	TqId   int64  `json:"tq_id" xorm:"tq_id"`                  //章节Id
	TSum   int64  `json:"t_sum" xorm:"t_sum"`                  //总题数
	THasdo int64  `json:"t_hasdo" xorm:"t_hasdo"`              //已做人数
	DoAt   int64  `json:"do_at" xorm:"do_at"`                  //做题限制时间
	OwnDo  int64  `json:"owndo" xorm:"-"`                      //自己做的次数
	State  int    `json:"n_state" xorm:"state" form:"n_state"` //知识点启用不启用（0启用1 不启用）
	Sort   int    `json:"n_sort" xorm:"sort" form:"n_sort"`    //排序
}

func (this *TopicNameRe) TableName() string {
	return "toplibnamere"
}

func (this *TopicNameRe) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *TopicNameRe) Get() (bool, error) {
	return Db(0).Get(this)
}

func (this *TopicNameRe) GetName() (bool, error) {
	w := fmt.Sprintf("type_id = %d and name = '%s'", this.TypeId, this.Name)
	return Db(0).Where(w).Get(this)
}

func (this *TopicNameRe) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return Db(0).Where(where).Delete(this)
}

func (this *TopicNameRe) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *TopicNameRe) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *TopicNameRe) HasSumSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

// 更新章节练习被多少人用过
func (this *TopicNameRe) HasSumSet1() ([]TopicNameRe, error) {

	list := make([]TopicNameRe, 0)
	err := Db(0).SQL("SELECT SUM(t_hasdo) t_hasdo, tq_id from toplibnamere	WHERE tq_id>0	GROUP BY tq_id").Find(&list)
	return list, err
}
func (this *TopicNameRe) Count(where string) (int64, error) {
	var val TopicNameRe
	return Db(0).Where(where).Count(&val)
}

func (this *TopicNameRe) where(fage, count, page int, where, field string) ([]TopicNameRe, error) {
	list := make([]TopicNameRe, 0)
	var err error
	if field == "" {
		field = "id"
	}
	page--
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

func (this *TopicNameRe) TList(page *PageFields) (int64, []TopicNameRe, error) {
	var tnList []TopicNameRe
	where := fmt.Sprintf("sid >= %d and sid< %d and state=0", this.SId-(this.SId%100000), this.SId-(this.SId%100000)+100000)
	if this.TypeId != 0 {
		where += fmt.Sprintf(" and type_id=%d  ", this.TypeId)
	}
	tbl := Db(0).Table(this.TableName()).Desc("state").Where(where)

	total, err := tbl.Clone().Count()
	if err != nil {
		return 0, nil, err
	}
	err = tbl.Desc("at").Limit(page.Pnum, (page.Page-1)*page.Pnum).Find(&tnList)

	if err != nil {
		return 0, nil, err
	}
	return total, tnList, nil
}
func (this *TopicNameRe) TIdList() ([]int64, error) {
	tnList := make([]int64, 0)
	where := fmt.Sprintf("sid >= %d and sid< %d", this.SId-(this.SId%100000), this.SId-(this.SId%100000)+100000)
	if this.TypeId != 0 {
		where += fmt.Sprintf(" and type_id=%d ", this.TypeId)
	}
	err := Db(0).Table(this.TableName()).Cols("id").Where(where).Find(&tnList)
	return tnList, err
}
func (this *TopicNameRe) GetList() ([]TopicNameRe, error) {
	topicNames := make([]TopicNameRe, 0)
	err := Db(0).Find(&topicNames)
	return topicNames, err
}

type TqCount struct {
	TqId int64 `json:"-" xorm:"tq_id"`  //章节id
	TSum int64 `json:"-"  xorm:"t_sum"` //题量总数
}

func (this *TopicNameRe) GetTqCount([]int64) ([]TqCount, error) {

	tqCounts := make([]TqCount, 0)
	err := Db(0).Table(this.TableName()).Select("SUM(t_sum) t_sum ,tq_id ").GroupBy("tq_id").Where("state=0").Find(&tqCounts)
	return tqCounts, err

}

func (this *TopicNameRe) GetListByTqid() ([]int64, error) {
	tidList := make([]int64, 0)
	err := Db(0).Table(this.TableName()).Select("id").Where("tq_id=? and state=0 ", this.TqId).Find(&tidList, &this)
	return tidList, err
}

func (this *TopicNameRe) AddHasDo() {
	this.update(fmt.Sprintf(" id=%d ", this.Id), "t_hasdo")
}
